package com.whale.cloud.util.hbase;

import com.whale.cloud.util.hbase.sql.engine.impl.HBaseSqlEngineImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

/** Created by jiaoshiru on 2017/5/25. */
@org.springframework.context.annotation.Configuration
@Log4j2
@EnableConfigurationProperties(HBaseProperties.class)
@Import({HBaseSqlEngineImpl.class})
public class HbaseAutoConfiguration {

  private static final String HBASE_MASTER = "hbase.master";
  private static final String HBASE_SUPERUSER = "hbase.superuser";
  private static final String HBASE_PROTECTION = "hbase.rpc.protection";
  private static final String ZOOKEEPER_PARENT = "zookeeper.znode.parent";
  private static final String HBASE_CLIENTPORT = "hbase.zookeeper.property.clientPort";
  private static final String HBASE_QUORUM = "hbase.zookeeper.quorum";

  @Autowired private HBaseProperties hBaseProperties;
  @Autowired private HBaseSqlEngineImpl hBaseSqlEngineImpl;

  /**
   * 原生 hbase-client
   *
   * @return
   * @throws Exception
   */
  @Bean(name = "hbaseClient")
  @Qualifier("hbaseClient")
  public HbaseClient hbaseConnection() throws Exception {
    Configuration configuration = HBaseConfiguration.create();
    log.info("----HBASE_MASTER----" + this.hBaseProperties.getMaster());
    configuration.set(HBASE_MASTER, this.hBaseProperties.getMaster());
    configuration.set(HBASE_SUPERUSER, this.hBaseProperties.getSuperuser());
    configuration.set(HBASE_PROTECTION, this.hBaseProperties.getProtection());
    configuration.set(ZOOKEEPER_PARENT, this.hBaseProperties.getParent());
    configuration.set(HBASE_CLIENTPORT, this.hBaseProperties.getClientPort());
    configuration.set(HBASE_QUORUM, this.hBaseProperties.getQuorum());

    HbaseClient hbaseClient = new HbaseClient();
    hbaseClient.setConnection(ConnectionFactory.createConnection(configuration));
    return hbaseClient;
  }

  //  @Bean(name = "hBaseSqlEngine")
  //  @Qualifier("hBaseSqlEngine")
  //  public HBaseSqlEngine hBaseSqlEngine() {
  //    log.info("--HBaseSqlEngine---");
  //    return hBaseSqlEngineImpl;
  //  }
  /**
   * Spring-data-hadoop-hbase
   *
   * @return
   * @throws Exception
   */
  //@Bean(name = "springHbaseClient")
  //@Qualifier("springHbaseClient")
  /*public SpringHbaseClient hbaseConnections() throws Exception {
      Configuration configuration = HBaseConfiguration.create();
      configuration.set(HBASE_MASTER, this.hBaseProperties.getMaster());
      configuration.set(HBASE_SUPERUSER, this.hBaseProperties.getSuperuser());
      configuration.set(HBASE_PROTECTION, this.hBaseProperties.getProtection());
      configuration.set(ZOOKEEPER_PARENT, this.hBaseProperties.getParent());
      configuration.set(HBASE_CLIENTPORT, this.hBaseProperties.getClientPort());
      configuration.set(HBASE_QUORUM, this.hBaseProperties.getQuorum());

      SpringHbaseClient springHbaseClient = new SpringHbaseClient();
      springHbaseClient.setConfiguration(configuration);
      springHbaseClient.setHbaseTemplate(new HbaseTemplate(configuration));
      return springHbaseClient;
  }*/
}
